// pages/add/add.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        itemCon:true,
        name:"",
        phone:"",
        address:"",
        index:null
    },
    edit(){
        wx.navigateTo({
            url:"/pages/address/address"
        })
        let number = this.data.index
        console.log(number);
        var ab = wx.getStorageSync('mation');
        ab[number].name =  this.data.name;
        ab[number].phone =  this.data.phone;
        ab[number].address =  this.data.address;
            wx.setStorageSync('mation', ab);
            wx.navigateBack({
                delta: 2
              })
    },
    name(event){
        let n = event.detail.value  
        this.setData({
            name : n
        })
    },
    phone(event){
        let p = event.detail.value  
        this.setData({
            phone : p
        })
    },
    address(event){
        let a = event.detail.value  
        this.setData({
            address : a
        })
    },
    preservation(){
        wx.navigateTo({
            url:"/pages/address/address"
        })
        let information = {
            name:this.data.name,
            phone:this.data.phone,
            address:this.data.address
        }
        let mation = wx.getStorageSync("mation");
        if(mation){
            let index = mation.findIndex(item=>item.phone == this.data.phone);
            if(index != -1){ 
                mation.splice(index,1)
            }else{
                mation.push(information)
            }
             wx.setStorageSync('mation',mation);
        }else{
            wx.setStorageSync('mation', [information]);
        };
        wx.navigateBack({
            delta: 2
          })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let index = options.index;
        var bbc = wx.getStorageSync('mation');
        this.setData({
            name:bbc[index].name,
            phone:bbc[index].phone,
            address:bbc[index].address,
            itemCon:false,
            index:index
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})